package org.gangcai.mac.shop.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wuhenzhizao.titlebar.utils.AppUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.AuthoContactsActivity;
import org.gangcai.mac.shop.activity.AuthoEducationActivity;
import org.gangcai.mac.shop.activity.AuthoICCardActivity;
import org.gangcai.mac.shop.activity.AuthoOperationActivity;
import org.gangcai.mac.shop.activity.AuthoQQActivity;
import org.gangcai.mac.shop.activity.AuthoTaobaoActivity;
import org.gangcai.mac.shop.activity.AuthoUserIdentifyActivity;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.QuotaBean;
import org.gangcai.mac.shop.bean.StepItem;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 额度界面
 */
public class QuotaFragment extends RxFragment implements MultiItemTypeAdapter.OnItemClickListener, OnRefreshListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.quotaTitle)
    TextView quotaTitle;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ArrayList<StepItem> stepItems;
    private CommonAdapter<StepItem> adapter;


    public QuotaFragment() {
        // Required empty public constructor
    }

    public static QuotaFragment newInstance(String param1, String param2) {
        QuotaFragment fragment = new QuotaFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_quota, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
//        setRecyclerViewData(null);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(this);
        loadData();
    }

    private void loadData() {
        RetrofitManager.create().my_quota(SPUtils.getInstance().getString("uid"))
                .compose(this.<QuotaBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<QuotaBean>(refreshLayout) {
                    @Override
                    public void onSuccess(QuotaBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            money.setText(value.getQuota_present());
                            setRecyclerViewData(value);
                        }
                    }
                });

    }

    private void setRecyclerViewData(QuotaBean bean) {
        stepItems.clear();
        stepItems.add(new StepItem(bean.getIs_realname(), "实名认证", R.mipmap.name_u, R.mipmap.name_u_un));

        stepItems.add(new StepItem(bean.getIs_bankident(), "银行卡", R.mipmap.card_u, R.mipmap.card_u_un));

        stepItems.add(new StepItem(bean.getIs_operator(), "运营商", R.mipmap.business_u, R.mipmap.business_u_un));

        stepItems.add(new StepItem(bean.getIs_contacts(), "联系人", R.mipmap.contacts_u, R.mipmap.contacts_u_un));

        stepItems.add(new StepItem(bean.getIs_culture(), "文化程度", R.mipmap.culture_u, R.mipmap.culture_u_un));

        stepItems.add(new StepItem(bean.getIs_taobao(), "淘宝认证", R.mipmap.taobao_u, R.mipmap.taobao_u_un));


        stepItems.add(new StepItem(bean.getIs_qq(), "QQ审核认证", R.mipmap.qq_u, R.mipmap.qq_u_un));

        adapter.notifyDataSetChanged();
    }

    private void initView() {
        ((LinearLayout.LayoutParams) quotaTitle.getLayoutParams()).topMargin = AppUtils.getStatusBarHeight(getActivity());
        recyclerview.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        stepItems = new ArrayList<>();
        adapter = new CommonAdapter<StepItem>(getActivity(), R.layout.fragment_quota_recycleview_item, stepItems) {
            @Override
            protected void convert(ViewHolder holder, StepItem o, int position) {
                if (position == 0) {
                    holder.getView(R.id.solideTopV).setVisibility(View.INVISIBLE);
                } else if (position == stepItems.size() - 1) {
                    holder.getView(R.id.solideBottomV).setVisibility(View.INVISIBLE);
                } else {
                    holder.getView(R.id.solideTopV).setVisibility(View.VISIBLE);
                    holder.getView(R.id.solideBottomV).setVisibility(View.VISIBLE);

                }
                ((ImageView) holder.getView(R.id.stepIcon)).setImageResource(o.getLeftIcon());
                holder.setText(R.id.msg, o.getMsg());
                holder.setText(R.id.rightTips, o.getRightTips());
                if (o.isCeritify()) {
                    //已认证

                    ((TextView) holder.getView(R.id.msg)).setTextColor(getResources().getColor(R.color.textColorPrimary));
                    ((TextView) holder.getView(R.id.rightTips)).setTextColor(getResources().getColor(R.color.colorPrimary));
                } else {
                    ((TextView) holder.getView(R.id.msg)).setTextColor(getResources().getColor(R.color.textColorDark));
                    ((TextView) holder.getView(R.id.rightTips)).setTextColor(getResources().getColor(R.color.textColorDark));
                }

            }


        };
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        switch (position) {
            case 0:
                //用户身份认证
                ActivityUtils.startActivity(this.getActivity(), AuthoUserIdentifyActivity.class);
                break;
            case 1:
                //银行卡
                ActivityUtils.startActivity(this.getActivity(), AuthoICCardActivity.class);
                break;
            case 2:
                //运营商
                ActivityUtils.startActivity(this.getActivity(), AuthoOperationActivity.class);
                break;
            case 3:
                //联系人
                ActivityUtils.startActivity(this.getActivity(), AuthoContactsActivity.class);
                break;
            case 4:
                //文化程度
                ActivityUtils.startActivity(this.getActivity(), AuthoEducationActivity.class);
                break;
            case 5:
                //淘宝认证
                ActivityUtils.startActivity(this.getActivity(), AuthoTaobaoActivity.class);
                break;
            case 6:
                //qq审核认证
                ActivityUtils.startActivity(this.getActivity(), AuthoQQActivity.class);
                break;
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onReceiveEvent(EventBusBaseBean1 bean) {
        loadData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData();
    }
}
